"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
import jsonpath

from litemall.apis.base_api import BaseApi


class GoodsApi(BaseApi):

    # 上架商品
    def create(self, goods_sn: str, goods_name: str):

        # logger.info(f"商品编号：{goods_sn}, 商品名称：{goods_name}")

        info = {
            "url": f"{self.base_url}/admin/goods/create",
            "method": "post",
            "headers": {"X-Litemall-Admin-Token": self.token},
            "json": {
                "goods": {
                    "goodsSn": goods_sn,
                    "name": goods_name,
                },
                "specifications": [
                    {
                        "specification": "规格",
                        "value": "标准",
                        "picUrl": ""
                    }
                ],
                "products": [
                    {
                        "id": 0,
                        "specifications": [
                            "标准"
                        ],
                        "price": 0,
                        "number": 0,
                        "url": ""
                    }
                ],
                "attributes": [
                    {"attribute": "材质", "value": "纯棉"
                     }]}
        }
        r = self.do_request(info)
        # logger.info(r.json())
        return r

    # 查询商品
    def query(self, goods_name: str = None, goods_sn: str = None, goods_id: str = None):
        info = {
            "url": f"{self.base_url}/admin/goods/list",
            "method": "GET",
            "headers": {"X-Litemall-Admin-Token": self.token},
            "params": {
                "name": goods_name,
                "goodsSn": goods_sn,
                "goodsId": goods_id,
            }
        }
        return self.do_request(info)

    # 删除商品
    def delete(self, goods_id: int):
        info = {
            "url": f"{self.base_url}/admin/goods/delete",
            "method": "post",
            "headers": {"X-Litemall-Admin-Token": self.token},
            "json": {"id": goods_id}
        }
        return self.do_request(info)

    def do_find_id(self, goods_name: str):
        r1 = self.query(goods_name=goods_name)
        result = jsonpath.jsonpath(r1.json(), "$..id")
        if result and len(result) == 1:
            my_id = result[0]
        else:
            return None
        return my_id
